var isClickFlag = false;
$(document).ready(function () {

    //渲染页面内容及样式
    loadPage();

    //生成验证码
    create_code();

});

/**
 * 渲染页面内容及样式
 */
function loadPage(){

    var zsmc = $("#zsmc").val();
    localStorage.setItem("zsmc", zsmc);

    var yyms = $("#yyms").val();
    localStorage.setItem("yyms", yyms);

    var yylogo = $("#yylogo").val();
    if(yylogo != null && yylogo != ''){
        $("#imgLogo").attr("src", "commonapi/showFile?busiCol=YYLOGO");
    }else{
        $("#imgLogo").attr("src", "img/login-logo.png");
    }

    var dltp = $("#dltp").val();
    if(dltp != null && dltp != ''){
        $("#loginLeftDiv").attr("style","background:url('commonapi/showFile?busiCol=DLTP') no-repeat center;");
    }else{
        $("#loginLeftDiv").attr("style","background:url('img/login-left.png') no-repeat center;");
    }

    var dlbj = $("#dlbj").val();
    if(dlbj != null && dlbj != ''){
        $("body").attr("style","background:url('commonapi/showFile?busiCol=DLBJ') no-repeat center;" +
            "-webkit-background-size: cover;" +
            "-moz-background-size: cover;" +
            "-o-background-size: cover;" +
            "background-size: cover;");
    }else{
        $("body").attr("style","background:url('img/login-bg.png') no-repeat center;" +
            "-webkit-background-size: cover;" +
            "-moz-background-size: cover;" +
            "-o-background-size: cover;" +
            "background-size: cover;");
    }
}

/**
 * 生成验证码
 */
function create_code() {
    function shuffle() {
        var arr = ['1', 'r', 'Q', '4', 'S', '6', 'w', 'u', 'D', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'o', 'p',
            'q', '2', 's', 't', '8', 'v', '7', 'x', 'y', 'z', 'A', 'B', 'C', '9', 'E', 'F', 'G', 'H', '0', 'J', 'K', 'L', 'M', 'N', 'O', 'P', '3', 'R',
            '5', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        return arr.sort(function () {
            return (Math.random() - .5);
        });
    };
    shuffle();
    function show_code() {
        var ar1 = '';
        var code = shuffle();
        for (var i = 0; i < 4; i++) {
            ar1 += " "+ code[i];
        }
        $(".c-right").text(ar1);
    };
    show_code();
    $(".c-right").click(function () {
        $("#input-error").css("visibility", "hidden");
        show_code();
    });
}

/**
 * 登录操作
 */
function login() {
    if(isClickFlag){
        return;
    }
    $(".submit-btn").css("background-color","#707b95");
    isClickFlag = true;
    var username = $("#username").val();
    var password = $("#password").val();
    var code = $("#code").val();
    if (username == "" || password == "" || code == "") {
        errorMsg("账号或密码或验证码为空,请重新输入！");
    } else if ($('#code').val().toLowerCase() != $("#code-produce").text().toLowerCase().replace(/\s/ig,'')) {
        errorMsg("验证码有误,请重新输入！");
    }
    else {
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "userLogin?username=" + username + "&password=" + password,
            success: function (jsonData) {
                if (jsonData.statusCode == 200) {//登录成功
                    localStorage.setItem("token", jsonData.repData.token);
                    localStorage.setItem("userName", jsonData.repData.userName);
                    localStorage.setItem("zclsh", jsonData.repData.zclsh);
                    localStorage.setItem("jgbm", jsonData.repData.jgbm);
                    window.location.href="index";
                } else {
                    errorMsg(jsonData.repMsg);
                    $("#submit").click("login");
                }
            }
        });
    }
}

/**
 * 错误提示
 * @param msg
 */
function errorMsg(msg){
    $("#input-error").children()[1].innerHTML = msg;
    $("#input-error").css("visibility", "visible");
    $(".submit-btn").css("background-color","#3769e4");
    isClickFlag = false;
}

/**
 * 实现回车自动登录
 */
$(document).keydown(function (event) {
    if (event.keyCode == 13) {
        $("#submit").click();
    }
});



